export function averageOfLevels(root: TreeNode | null): number[] {
  if (!root) return [];
  const res: number[] = [];
  const nodeArr: TreeNode[]  = [root];
  while (nodeArr.length) {
    const len = nodeArr.length
    let sum:number = 0 
    for (let index = 0; index < len; index++) {
        const node = nodeArr.shift() 
        sum = sum + node.val  
        if(node.left) nodeArr.push(node.left)
        if(node.right) nodeArr.push(node.right)
    }
    const avg = sum /len
    res.push(avg)
  }
  return res;
}
